#!/usr/bin/env python

import sys
import glob
import os
import xseg.segCombine as segCombine
import xseg.lung_mask as lung_mask
import xseg.automaticSeg as xseg
import xseg.adaSeg as adaSeg
import xseg.bone_extract as bone_extract
import xseg.vas_seg as vas_seg
def getParser():
    parser = OptionParser()
    parser.add_option("-i","--img",dest='fname',
                      help='name of image to read')
    parser.add_option("-t","--thresh",dest='threshold',nargs=2,default=(0,10000),type='int')
    parser.add_option("-s","--seed",dest='seed',nargs=3,default=(0,0,0),
                      type='int')
    parser.add_option("-v", "--variance", dest='var', default=1.0, type = 'float')
    parser.add_option("-m", "--mode", dest = 'mode', nargs = 5, default = 1, type = 'int')

    return parser

def run():
   presentDir = "/HD2/xiaofei/Code2"
   os.chdir(presentDir)
   dirs = glob.glob("PE00024") 
   print dirs

   for d in dirs:
      os.chdir(str(d))
      os.system("pwd")
      fname = str(d) + ".mha" 
      print fname
      automaticSeg.seg(fname)
      lungMask = lung_mask.lung_mask(fname)
      outputfile = adaSeg.adaSeg("m_1s_1.mha", fname, lungMask )
      
      counter = adaSeg.count(outputfile)
      if counter > 3000000 : 
           bone_mask = bone_extract.main(fname)
             
      
      file1 = glob.glob("*vascSeg.mha") 
      file2 = outputfile
      file3 = str(d) + "Filter1_seg.mha"
      cmd = "addImages " + file1[0] + " " + file2 + " " + file3
      print cmd
      os.system(cmd) 
      print cmd
      os.chdir(presentDir)
  
def main():
   run()
if __name__ == '__main__':
    main()
